package com.sdcqjy.property.mode;

import android.content.Context;
import android.text.TextUtils;

import com.google.gson.Gson;
import com.google.gson.annotations.SerializedName;
import com.sdcqjy.mylibrary.PrivateFileUtils;
import com.sdcqjy.property.presenter.contract.a;

import java.io.File;

/**
 * Created by 磁磁帅 on 2018/6/12.
 * 功能：
 */

public class AppLev {
    /**
     * appDownLoadPath : /leinuo/resource/app/com.sdcqjy.property.apk
     * edition : 1.0.1
     * reason : 更改了一些bug
     */
    @SerializedName("appDownLoadPath")
    private String url;
    @SerializedName("edition")
    public String version;
    @SerializedName("reason")
    public String content;

    private String apkPath;
    private transient File installApk;

    public String getUrl() {
        if (!url.contains("://"))
            url = a.FileHost + url;
        return url;
    }

    public File getInstallApk() {
        if (installApk == null && !TextUtils.isEmpty(apkPath)) {
            installApk = new File(apkPath);
        }
        return installApk;
    }

    public void setInstallApk(File installApk) {
        this.installApk = installApk;
        apkPath = installApk.getAbsolutePath();
    }

    public boolean equVersion(String version) {
        return version != null && version.equals(this.version);
    }

    //-------------保存数据-------------
    private static AppLev mode;
    private static final String jsonName = "AppLev.json";

    public static AppLev getMode(Context context) {
        if (mode == null) {
            try {
                String txt = new PrivateFileUtils(context, jsonName).getString();
                mode = new Gson().fromJson(txt, AppLev.class);
            } catch (Exception e) {
            }
        }
        //调试使用
        //mode.token = "eGllY2ljaTE1MDgzMzM3Mzc=";
        return mode;
    }

    public static void setMode(Context context, AppLev mode) {
        if (mode == null) return;
        AppLev.mode = mode;
        try {
            String s = new Gson().toJson(mode);
            new PrivateFileUtils(context, jsonName).setString(s);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void clear(Context context) {
        mode = null;
        new PrivateFileUtils(context, jsonName).delete();
    }
}
